﻿using SuperSocialShare.Authorize;
using System;
using System.IO;
using System.IO.IsolatedStorage;

namespace SuperSocialShare.Helper
{
    public class AuthorizeManager
    {
        private static string FolderName = "authorizes";

        /// <summary>
        /// 保存指定类型的授权信息
        /// </summary>
        internal static void Save(AuthorizeInfo info, SocialType type)
        {
            info.AuthorizeDate = DateTime.Now;
            string jsonstring = JsonConverter.GetJsonStringFromObject(info);
            if (!string.IsNullOrEmpty(jsonstring))
            {
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (!store.DirectoryExists(FolderName))
                        store.CreateDirectory(FolderName);
                    string filename = GetStorageFileName(type);
                    if (string.IsNullOrEmpty(filename))
                        return;
                    if (store.FileExists(filename))
                        store.DeleteFile(filename);
                    using (var isoFileStream = new IsolatedStorageFileStream(filename, FileMode.OpenOrCreate, store))
                    {
                        using (var isoFileWriter = new StreamWriter(isoFileStream))
                        {
                            isoFileWriter.Write(jsonstring);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 获取指定类型的授权信息
        /// </summary>
        /// <param name="type">社区类型</param>
        /// <returns></returns>
        public static AuthorizeInfo GetAuthorizeInfo(SocialType type)
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                string filename = GetStorageFileName(type);
                if (store.FileExists(filename))
                {
                    using (var isoFileStream = new IsolatedStorageFileStream(filename, FileMode.Open, store))
                    {
                        using (var isoFileReader = new StreamReader(isoFileStream))
                        {
                            string jsonstring = isoFileReader.ReadToEnd();
                            return JsonConverter.GetObjectFromJsonString(jsonstring, GetAuthorizeType(type)) as AuthorizeInfo;
                        }
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// 删除指定社区类型的授权信息
        /// </summary>
        /// <param name="type">社区类型</param>
        internal static void DeleteAuthorizeInfo(SocialType type)
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                string filename = GetStorageFileName(type);
                if (store.FileExists(filename))
                {
                    store.DeleteFile(filename);
                }
            }
        }

        /// <summary>
        /// 判断指定社区的授权是否有效
        /// </summary>
        /// <param name="type">社区类型</param>
        /// <returns></returns>
        public static bool IsAuthorizeValid(SocialType type)
        {
            AuthorizeInfo info = GetAuthorizeInfo(type);
            if (info != null)
            {
                int seconds = (int)System.DateTime.Now.Subtract(info.AuthorizeDate).TotalSeconds;
                if (seconds < info.Expires)
                    return true;
            }
            return false;
        }

        private static string GetStorageFileName(SocialType type)
        {
            string filename = string.Empty;
            switch (type)
            {
                case SocialType.Tencent:
                    filename = FolderName + "\\tencent.authorize.info";
                    break;
                case SocialType.Sina:
                    filename = FolderName + "\\sina.authorize.info";
                    break;
                case SocialType.SoHu:
                    filename = FolderName + "\\sohu.authorize.info";
                    break;
                case SocialType.Renren:
                    filename = FolderName + "\\renren.authorize.info";
                    break;
                case SocialType.KaiXin:
                    filename = FolderName + "\\kaixin.authorize.info";
                    break;
                case SocialType.NetEase:
                    filename = FolderName + "\\netease.authorize.info";
                    break;
                default:
                    break;
            }
            return filename;
        }

        private static Type GetAuthorizeType(SocialType type)
        {
            Type authorizeType = typeof(AuthorizeInfo);
            switch (type)
            {
                case SocialType.Tencent:
                    authorizeType = typeof(TencentAuthorizeInfo);
                    break;
                case SocialType.Sina:
                    authorizeType = typeof(SinaAuthorizeInfo);
                    break;
                case SocialType.SoHu:
                    authorizeType = typeof(SoHuAuthorizeInfo);
                    break;
                case SocialType.Renren:
                    authorizeType = typeof(RenrenAuthorizeInfo);
                    break;
                case SocialType.KaiXin:
                    authorizeType = typeof(KaixinAuthorizeInfo);
                    break;
                case SocialType.NetEase:
                    authorizeType = typeof(NetEaseAuthorizeInfo);
                    break;
                default:
                    break;
            }
            return authorizeType;
        }


    }
}
